<?php
/**

* @version $Id: controller.php,v 1.4 2009/06/04 20:13:37 dgravato Exp $

* @package    sketchup

* @subpackage

* @author     Chris Borkowski {@link http://www.guggenheim.org}

* @author     Created on 30-Mar-2009

*/

// no direct access

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

/**
 * sketchup Controller
 *
 * @package    sketchup
 * @subpackage Controllers
 */

// I hate controllers
class SketchupController extends JController
{

	function display()
	{
		$document =& JFactory::getDocument();
		$viewName = JRequest::getVar('view', 'all');
		$viewType = $document->getType();
		$view = &$this->getView($viewName, $viewType, 'View');
		$model =& $this->getModel( $viewName, 'ModelSketchups' );

		if (!JError::isError( $model )) {
			$view->setModel( $model, true );
		}

		$view->setLayout('default');
		$view->display();
	}


	function comment()
	{
		global $option;

		$row =& JTable::getInstance('comment', 'Table');

		if (!$row->bind(JRequest::get('post'))) {
      JError::raiseError(500, $row->getError() );
      exit;
		}

		$row->id = null;
		$row->sketchup_id = (int) $row->sketchup_id;
		$row->user_id = (int) $row->user_id;

		$currDate =& JFactory::getDate();
		$row->comment_date = $currDate->toMySQL();

		$user =& JFactory::getUser();

		if($user->id)
		{
			$row->user_id = $user->id;
		} else {
			$row->user_id = 0;
		}

		if (!$row->store()) {
      JError::raiseError(500, $row->getError() );
      exit;
		}

		$this->setRedirect(JRoute::_('index.php?option=' . $option .'&id=' . $row->sketchup_id . '&view=project'), 'Comment Added.');
	}

	function tag()
	{
		global $option;

		$row =& JTable::getInstance('tag', 'Table');

		if (!$row->bind(JRequest::get('post'))) {
      JError::raiseError(500, $row->getError() );
      exit;
		}

		$row->id = null;
		$row->sketchup_id = (int) $row->sketchup_id;

		$currDate =& JFactory::getDate();
		$row->tag_date = $currDate->toMySQL();
		//we'll never use this

		if (!$row->store()) {
      JError::raiseError(500, $row->getError() );
      exit;
		}


		$this->setRedirect(JRoute::_('index.php?option=' . $option . '&view=project'. '&id='. $row->sketchup_id), 'Tag Added.');

	}

		
    /**
     *  Method of the voting
     *  this is incompleate
     * @access public
     * @since never
    */

    function vote()
    {

    global $option;

    $session =& JFactory::getSession();
    
    $row =& JTable::getInstance('Vote', 'Table');
    $row->sketchup_id = JRequest::getInt('sketchup_id', 0);
    $row->ip_address = JRequest::getString('REMOTE_ADDR', '', 'server');
    $row->session_id = $session->getId();
    $row->time = time();

    $Itemid = JRequest::getInt('Itemid', 0);

    // cookie doesn't expire for 10 years
    $cookieExpire = time()+60*60*24*3650;

    if (!$row->check()) {
      $this->setRedirect(JRoute::_('index.php?option=' . $option . '&view=vote&already_voted=1&Itemid='. $Itemid), 'Vote already recorded.');
    } else {
      if (!$row->store()) {
        JError::raiseError(500, $row->getError() );
        exit;
      }
      
      $this->setRedirect(JRoute::_('index.php?option=' . $option . '&view=thanks'));
    }

    // set cookie regardless: if someone deletes the cookie, reset it.

	setcookie( JUtility::getHash('sketchup'), JUtility::getHash($row->session_id), $cookieExpire, '/' );

	}


} //the end
